package com.star.service.impl;

import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.star.dao.NewsDao;
import com.star.model.NewsModel;
import com.star.service.NewsService;
import com.star.utils.DateUtils;
import com.star.utils.PageQuery;
import com.star.utils.StrUtils;

@Service
public class NewsServiceImpl implements NewsService {

	@Autowired
	public NewsDao newsDao;

	@Override
	public List<NewsModel> getNewsPageQuery(PageQuery pageQuery) {
		return newsDao.getNewsPageQuery(pageQuery);
	}

	@Override
	public List<NewsModel> queryNewsByKeyId(Integer keyId) {
		return newsDao.queryNewsByKeyId(keyId);
	}

	@Override
	public List<NewsModel> getNewsPageQueryByDate(Date date, PageQuery pageQuery) {
		if (date == null) {
			return null;
		}
		return newsDao.getNewsPageQueryByDate(date, pageQuery.getPageStar(), pageQuery.getPageSize());
	}

	@Override
	public List<NewsModel> querySearchPageQuery(String date, String search, PageQuery pageQuery) {
		List<NewsModel> newsList = null;
		if ("all".equals(date)||StrUtils.isEmpty(date)) {
			if (StrUtils.isNotEmpty(search)) {
				newsList = newsDao.getNewsSearchPageQuery(search,pageQuery.getPageStar(), pageQuery.getPageSize());
			} else {
				newsList = newsDao.getNewsPageQuery(pageQuery);
			}
		} else {
			Date tempDate = new DateUtils().afterHour(-Integer.parseInt(date), new Date());
			if (StrUtils.isNotEmpty(search)) {
				newsList = newsDao.getNewsSearch(tempDate, search, pageQuery.getPageStar(), pageQuery.getPageSize());
			} else {
				newsList = newsDao.getNewsPageQueryByDate(tempDate, pageQuery.getPageStar(), pageQuery.getPageSize());
			}
		}
		
		if ((newsList != null && newsList.size() > 0)) {
			for (NewsModel newsM : newsList) {
				DateUtils utils = new DateUtils();
				newsM.setPdate(utils.howLong2("Hm", DateUtils.formateDate(new Date()),
						DateUtils.formateDate(newsM.getGmt_create())));
			}
		}
		return newsList;
	}

}
